package cn.com.surker.production.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.ProductionContractTypeEnum;
import cn.com.surker.oa.dao.OaProductionContractDao;
import cn.com.surker.oa.vo.ProductionContractVo;
import cn.com.surker.production.entity.ProductionTransportSubsidy;
import cn.com.surker.production.dao.ProductionTransportSubsidyDao;
import cn.com.surker.production.service.TranSubsidyService;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.surker.spring.RestResultDto;

import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* <p>
    * 运输合同补贴表 服务实现类
    * </p>
*
* @author admin
* @since 2024-12-05
*/
public abstract class TranSubsidyBaseServiceImpl extends ServiceImpl<ProductionTransportSubsidyDao, ProductionTransportSubsidy> implements TranSubsidyService {

    @Resource
    protected ProductionTransportSubsidyDao contractSubsidyDao;
    @Resource
    protected OaProductionContractDao productionContractDao;

    /**
     * 查询生产合同泵送补贴信息
     * @param contractId
     * @return
     */
    public Map<String, List<ProductionTransportSubsidy>> findByContract(Long contractId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<ProductionTransportSubsidy> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        query.orderByDesc("take_time");
        List<ProductionTransportSubsidy> priceList = contractSubsidyDao.selectList(query);
        if (priceList == null || priceList.size() == 0)
            return null;
        Map<String, List<ProductionTransportSubsidy>> map = new LinkedHashMap<>();
        for (int i = 0; i < priceList.size(); i++) {
            ProductionTransportSubsidy price = priceList.get(i);
            String takeTime = DateUtil.convertString(price.getTakeTime());
            if (map.containsKey(takeTime)) {
                List<ProductionTransportSubsidy> tempList = map.get(takeTime);
                tempList.add(price);
                map.put(takeTime, tempList);
            } else {
                List<ProductionTransportSubsidy> tempList = new ArrayList<>();
                tempList.add(price);
                map.put(takeTime, tempList);
            }
        }
        return map;
    }

    /**
     * 新增生产合同泵送补贴信息
     * @return
     */
    public RestResultDto savePrice(ProductionTransportSubsidy pumpSubsidy) {
        if (pumpSubsidy == null)
            throw new MessageCenterException("参数错误");
        if (StringUtils.isNull(pumpSubsidy.getOrgId()))
            throw new MessageCenterException("合同公司信息为空");
        if (StringUtils.isNull(pumpSubsidy.getContractId()))
            throw new MessageCenterException("合同信息为空");
        if (StringUtils.isNull(pumpSubsidy.getTranType()))
            throw new MessageCenterException("标号运输类型信息为空");
        if (StringUtils.isNull(pumpSubsidy.getCarModel()))
            throw new MessageCenterException("车辆类型为空");
        if (StringUtils.isNull(pumpSubsidy.getCountType()))
            throw new MessageCenterException("计算方式为空");
        if (StringUtils.isNull(pumpSubsidy.getPrice()))
            throw new MessageCenterException("运输单价为空");
        if (StringUtils.isNull(pumpSubsidy.getTakeTime()))
            throw new MessageCenterException("生效时间为空");

        Date nowDate = new Date();
        ProductionContractVo contract = productionContractDao.selectByIdVo(pumpSubsidy.getContractId());
        if (contract == null || contract.getStatus().intValue() == 1)
            throw new MessageCenterException("运输合同不存在或已被删除");
        if (contract.getType().intValue() != ProductionContractTypeEnum.TRANSPORT.getCode())
            throw new MessageCenterException("运输合同类型错误");
//        ProductionTransportPrice transportPrice = new ProductionTransportPrice(orgId, contractId, supplierId, takeDate,
//                transportType, countType, carModel, isSpeed, isLimit, minDistance, maxDistance, minStere, maxStere,price);
        pumpSubsidy.setCreateTime(nowDate);
//        transportPrice.setCreator(creator);
//        transportPrice.setRemark(remark);
//        transportPrice.setStatus(0);
        ProductionTransportSubsidy tempTransportPrice = contractSubsidyDao.findTakePrice(pumpSubsidy);
        if (tempTransportPrice != null) {
            tempTransportPrice.setUpdateTime(nowDate);
            tempTransportPrice.setUpdator(pumpSubsidy.getCreator());
            tempTransportPrice.setPrice(pumpSubsidy.getPrice());
            tempTransportPrice.setPriceType(pumpSubsidy.getPriceType());
            tempTransportPrice.setTakeTime(pumpSubsidy.getTakeTime());
            if (!StringUtils.isNull(pumpSubsidy.getExpireTime()))
                tempTransportPrice.setExpireTime(pumpSubsidy.getExpireTime());
            Integer code = contractSubsidyDao.updateById(tempTransportPrice);
            if (code > 0) {
                return RestResultDto.success("保存成功");
            }
            return RestResultDto.fail("保存运输补贴异常");
        }
        Integer code = contractSubsidyDao.insert(pumpSubsidy);
        if (code > 0) {
            return RestResultDto.success("保存成功");
        }
        return RestResultDto.fail("保存运输单价异常");
    }
    /**
     * 修改泵送补贴信息
     * @return
     */
    public RestResultDto updatePrice(ProductionTransportSubsidy pumpSubsidy) {
        if (pumpSubsidy == null || StringUtils.isNull(pumpSubsidy.getId()))
            throw new MessageCenterException("参数错误");
        ProductionTransportSubsidy pumpPrice = contractSubsidyDao.selectById(pumpSubsidy.getId());
        if (pumpPrice == null || pumpPrice.getStatus() == 1)
            throw new MessageCenterException("生产泵送补贴不存在或该单价已被禁用");
//        ProductionTransportPrice newTransportPrice = new ProductionTransportPrice(transportPrice.getOrgId(), transportPrice.getContractId(),
//                transportPrice.getSupplierId(), transportPrice.getTakeTime(), transportType, countType, carModel,
//                isSpeed, isLimit, minDistance, maxDistance, minStere, maxStere,price);
        Date nowDate = new Date();
        pumpSubsidy.setUpdateTime(nowDate);
        Integer code = contractSubsidyDao.updateById(pumpSubsidy);
        if (code > 0) {
            return RestResultDto.success("更新成功", pumpPrice);
        }
        return RestResultDto.fail("更新运输单价异常");
    }

    /**
     * 禁用合同
     * @param id
     * @param status
     * @return
     */
    public RestResultDto updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            throw new MessageCenterException("参数错误");
        ProductionTransportSubsidy transportPrice = contractSubsidyDao.selectById(id);
        if (transportPrice == null)
            throw new MessageCenterException("泵送补贴信息不存在");
        transportPrice.setStatus(status);
        transportPrice.setUpdateTime(new Date());
        transportPrice.setUpdator(creator);
        Integer code = contractSubsidyDao.updateById(transportPrice);
        if (code > 0) {
            return RestResultDto.success("更新成功", transportPrice);
        }
        return RestResultDto.fail("更新泵送补贴异常");
    }

	@Override
	public List<ProductionTransportSubsidy> queryTranPrice(Long tranConId, Integer tranType, Integer carModel, Integer roadType, Integer overLimit, Double distance, String tranTime) {
		if (tranConId == null || tranTime == null || distance == null) {
			return null;
		}
		QueryWrapper<ProductionTransportSubsidy> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (tranType != null) {
			qw.eq("tran_type", tranType);
		}
		if (carModel != null) {
			qw.eq("car_model", carModel);
		}
		if (roadType != null) {
			qw.eq("road_type", roadType);
		}
		if (overLimit != null) {
			qw.eq("over_limit", overLimit);
		}
		qw.eq("contract_id", tranConId);
		//	运输时间在价格时间之后
		qw.le("take_time", tranTime);
		List<ProductionTransportSubsidy> tranSubsidyList = contractSubsidyDao.selectList(qw);
		return tranSubsidyList;
	}
}